// pages/detail/detail.js
var util = require('../../utils/util.js');

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: null,
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain,
    ticketExpire: "",
    taoboaPwd: "",
    btnText: "",
    insureText: "",
    price2: "",
    postText: "",
    contentImg:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var goodsId = options.goodsId;
    //调用获取商品详情接口
    wx.request({
      url: app.globalData.apiDomain + '/api/goods/getGoodsDetail/' + goodsId,
      success(res) {
        var newDate = new Date(res.data.data.ticketExpire);
        var ticketExpir = null;
        var taoboaPwd = null;
        var nowTime = new Date();
        var btnText = "立即领券";
        var insureText = "";
        var price2 = res.data.data.price1;
        var postText = "包邮";
        if (res.data.data.noPostage == 0) {
          postText = "不包邮";
        }
        if (res.data.data.freightInsurance == 1) {
          insureText = "送运费险";
        }
        if (nowTime > newDate) {
          taoboaPwd = res.data.data.buyPwd;
          ticketExpir = "券已过期 仍可购买";
          btnText = "立即购买";
        } else {
          taoboaPwd = res.data.data.ticketPwd;
          ticketExpir = util.formatDate(newDate);
          price2 = res.data.data.price2;
        }
        var obj = JSON.parse(res.data.data.contentImg);
        that.setData({
          goods: res.data.data,
          ticketExpir: ticketExpir,
          taoboaPwd: taoboaPwd,
          btnText: btnText,
          insureText: insureText,
          price2: price2,
          postText: postText,
          contentImg: obj
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  goToTaoBao: util.throttle(function() {
    var that = this;
    wx.setClipboardData({
      data: that.data.taoboaPwd,
      success: function(res) {
        wx.showModal({
          title: '温馨提示',
          content: '复制成功！打开手机淘宝即可领券及下单！',
          cancelText: '使用教程',
          confirmText: '关闭',
          success: function(res) {
            if (res.confirm) {} else if (res.cancel) {
              wx.navigateTo({
                url: '../course/course',
              })
            }
          },
        })
      }
    })
  })
})